package com.wanmait.zaisousuo.controller;

import com.wanmait.zaisousuo.pojo.Admin;
import com.wanmait.zaisousuo.service.AdminService;
import com.wanmait.zaisousuo.util.GetVisitTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;

@Controller
@RequestMapping("backstage/manager")
public class ManagerController {
    @Resource
    private AdminService adminService;
    @RequestMapping("/login")
    public void login()
    {

    }
    @RequestMapping("/index")
    public void index()
    {

    }
    @RequestMapping("/welcome")
    public void welcome()
    {

    }
    @PostMapping("/login")
    public String login(Admin admin, HttpSession session, HttpServletRequest request)
    {
        Admin admin1=adminService.login(admin);

        if(admin1==null)
        {
            return "redirect:/backstage/manager/login?msg=err";
        }
        else
        {
            session.setAttribute("admin",admin1);
            String lastLoginIp = request.getRemoteAddr();
            Timestamp lastLoginTime = Timestamp.valueOf(GetVisitTime.visitTime());
            adminService.updateLoginInfo(lastLoginIp,lastLoginTime,admin1.getId());//修改本次登录时间和ip

            return "redirect:/backstage/manager/index";
        }
    }
}
